import { Controller } from "#Controllers/controller.js";
import { AuthService } from "#Services/index.js";
import { Log } from "#Libs/logs/logger.js";
import { Cache } from "#Libs/cache/cache.js";

class AuthController extends Controller {
    login = async (ctx) => {
        try {
            let userInfo = await AuthService.login(ctx.request.body);

            if (!userInfo?.isOk) {
                return this.error(ctx, userInfo?.code, userInfo?.msg, userInfo?.isOk);
            }
            return this.success(ctx, userInfo.data, "登录成功");
        } catch (error) {
            Log.error('登录失败', error);
            return this.error(ctx, 500, error.message);
        }
    }

    logOut = async (ctx) => {
        this.success(ctx, "退出成功");
    }

    register = async (ctx) => {
        try {
            await AuthService.register(ctx.request.body);
        } catch (error) {
            Log.error('注册失败', error);
            return this.error(ctx, 500, error.message);
        }
        this.success(ctx, "注册成功");
    }

    index = async (ctx) => {
        await Cache.put('test', 't', 20);

        // await Cache.set('test1', 't1', 'EX', 20);

        // await Cache.del('test');


        

        console.log(await Cache.get('test'));
        console.log(await Cache.get('test1'));
        this.success(ctx, ctx.state.user);
    }
    index1 = async (ctx) => {
        this.success(ctx, {...ctx.state.user, token: 5555});
    }
    index2 = async (ctx) => {
        this.success(ctx, {...ctx.state.user, token: 666});
    }
}

export default new AuthController();